package response;


import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/response")
public class ResponseStudyServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取请求发送的queryString数据: status = xxx
        String status = req.getParameter("status");
        //如果请求数据包含需要设置的状态码，才执行
        if(status != null) {
            resp.setStatus(Integer.parseInt(status));//把字符串转换为Integer类型
            resp.getWriter().write("设置响应状态码成功");
        }
        //设置响应头的键值对。键可以是标准的http响应头的键，也可以是自定义的
        //响应状态码是301,302,307，响应头有location字段，才是重定向
        resp.setHeader("Location","http://www.baidu.com");//这里我们没有设置响应状态码所以不会生效
        resp.setHeader("uesrname","张三");
    }
}
